/**
 *	@file	telseq_mic.c
 *	@brief	マイク　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/syswork.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_tr_mic.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

static u16 sub_MsgSelect(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* twk,u8 badge_num,BOOL akane_win);

/*
 *	@brief	電話　マイクの会話抽選
 */
u16 PMTelPlayL_Mic(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* twk)
{
	u8 badge_num;
	u8 akane_win;
	u16 ret;

	twk->seq_code = TEL_SEQ_SIMPLE;

	//ノーマル発信
	if(!twk->is_receive){

		//ラジオ塔占拠イベント中？
		if(SysWork_CheckDuaringTheRadioTowerEvent(man->ev_sv)){
			return TEL_TK_MIC_RECE_R_TOWER;
		}
		return TEL_TK_MIC_CALL_01+gf_rand()%3;
	}

	//ノーマル着信
	badge_num = MyStatus_GetBadgeCount(man->my_sv);
	akane_win = MyStatus_GetBadgeFlag(man->my_sv, BADGE_ID_T25 );

	ret  = sub_MsgSelect(man,twk,badge_num,akane_win);
	if(ret == 0xFFFF){	//フラグ無し
		if(!akane_win){
			return TEL_TK_MIC_RECE_DEF01;
		}else if(badge_num < 16){
			return TEL_TK_MIC_RECE_DEF02;
		}
		return TEL_TK_MIC_RECE_DEF03;
	}
	return ret;
}

static u16 sub_MsgSelect(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* twk,u8 badge_num,BOOL akane_win)
{
	if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL)){	//フラグ無し
		return 0xFFFF;
	}
	EventWork_ResetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL);	//フラグを落とす

	if( badge_num >= 16){ //バッジ16個？ ○
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_BADGE16)){
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_BADGE16);
			return TEL_TK_MIC_RECE_BADGE16;
		}
		return 0xFFFF;
	}
	if( MyStatus_GetBadgeFlag(man->my_sv, BADGE_ID_T11)){	//ナツメWin
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_NATSUME)){
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_NATSUME);
			return TEL_TK_MIC_RECE_NATSUME_WIN;
		}
		return 0xFFFF;
	}
	if( EventWork_CheckEventFlag(man->ev_sv,FE_R11_KABI_GET)){	//カビゴンWin ○
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_KABIGON)){
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_KABIGON);
			return TEL_TK_MIC_RECE_KABIGON;
		}
		return 0xFFFF;
	}
	if( EventWork_CheckEventFlag(man->ev_sv,FE_TEIKI_GET)){	//定期券ゲット○
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_TEIKIKEN)){
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_TEIKIKEN);
			return TEL_TK_MIC_RECE_TEIKIKEN_GET;
		}
		return 0xFFFF;
	}

	if(SysFlag_Arrive( man->ev_sv, SYSFLAG_MODE_CHECK, FLAG_ARRIVE_T06)){	//クチバ到着
		return TEL_TK_MIC_RECE_KUTIBA_ARRIVE;
	}

	if(SysFlag_GameClearCheck( man->ev_sv)){
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_DENDOU)){	//ゲームクリア
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_DENDOU);
			return TEL_TK_MIC_RECE_DENDOUIRI;
		}
		return 0xFFFF;
	}
	if(MyStatus_GetBadgeFlag(man->my_sv, BADGE_ID_T30 )){	//フスベバッジ入手
		return TEL_TK_MIC_RECE_FUSUBE_CLEAR;
	}
	if(EventWork_CheckEventFlag(man->ev_sv,FE_T25_RADIOSENKYO_END)){	//ラジオ塔占拠イベント終了
		return TEL_TK_MIC_RECE_R_TOWER_CLEAR;
	}
	if(EventWork_CheckEventFlag(man->ev_sv,FE_D35_RAJITO_END)){
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_RAJITO) ){	//ロケット団アジトクリア○
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_RAJITO);
			return TEL_TK_MIC_RECE_AJITO_CLEAR;
		}
		return 0xFFFF;
	}
	if(	EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_ASAGI_TOUDAI_CLEAR)){
		if(!EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_TOUDAI) ){	//灯台クリア○
			EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_MIC_TEL_TOUDAI);
			return TEL_TK_MIC_RECE_TOUDAI_CLEAR;
		}
		return 0xFFFF;
	}
	if(MyStatus_GetBadgeFlag(man->my_sv, BADGE_ID_T27 )){	//マツバWin
		return TEL_TK_MIC_RECE_MATSUBA_WIN;
	}
	if( akane_win ){	//アカネWin
		return TEL_TK_MIC_RECE_AKANE_WIN;
	}
	return 0xFFFF;
}